﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Entity;
using C2E2C.Entity;

namespace C2E2C.Data
{
    public class C2E2CDatabaseInitializer : DropCreateDatabaseIfModelChanges<C2E2CEntityContext>
    {
        protected override void Seed(C2E2CEntityContext context)
        {
            base.Seed(context);

            var accountSet = context.Set<Account>();
            Accounts.ToList()
                .ForEach(account => accountSet.Add(account));
        }

        private IEnumerable<Account> Accounts
        {
            get
            {
                yield return new Account()
                {
                    Id = 1,
                    UserName = "administrator",
                    Password = "123456",
                    RealName = "admin",
                    Email = "admin@hotmail.com",
                    Company = "c2e2c",
                    Phone = "13498771233"
                };
            }
        }
    }

    public class C2E2CDatabaseCreator : CreateDatabaseIfNotExists<C2E2CEntityContext>
    {
        protected override void Seed(C2E2CEntityContext context)
        {
            base.Seed(context);

            if (!context.Database.CreateIfNotExists())
            {
                throw new ApplicationException("数据库创建失败，请联系管理员");
            }
        }
    }
}
